#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

#define STRING_LENGTH 10

typedef struct {
    int weight;
    char color[STRING_LENGTH];
} mouse;

void swap_element(mouse[], int);
void quick_sort(mouse[], int);
void print_array(mouse[], int);

int main()
{
    int num;
    while (scanf("%d", &num) != EOF) {
        mouse* mouse_list = (mouse*)malloc(sizeof(mouse) * num);
        for (int i = 0; i < num; i++) 
            scanf("%d %s", &mouse_list[i].weight, mouse_list[i].color);

        quick_sort(mouse_list, num);
        print_array(mouse_list, num);
    }
}

void swap_element(mouse array[], int i, int j)
{
    int temp_weight;
    char temp_color[STRING_LENGTH];
    temp_weight = array[i].weight;
    strcpy(temp_color, array[i].color);
    array[i].weight = array[j].weight;
    strcpy(array[i].color, array[j].color);
    array[j].weight = temp_weight;
    strcpy(array[j].color, temp_color);
}

void quick_sort(mouse array[], int num)
{
    int i, last = 0;
    if (num <= 1)
        return;
    swap_element(array, 0, rand() % num);
    for (i = 1; i < num; i++) {
        if (array[i].weight > array[0].weight)
            swap_element(array, ++last, i);
    }

    swap_element(array, 0, last);
    quick_sort(array, last);
    quick_sort(array + last + 1, num - last - 1);
}

void print_array(mouse array[], int num)
{
    for (int i = 0; i < num; i++)
        printf("%s\n", array[i].color);
}